<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\user\controller;

use cmf\controller\RestUserBaseController;
use think\Db;
use think\Validate;
use api\user\model\TagModel as Tag;

class TagController extends RestUserBaseController
{

    public function getTag(Tag $Tag){
        $userId       = $this->getUserId();
        $data = $Tag->getList("userId =".$userId." or userId=0");
        $this->success('成功',$data);
    }
    /**
     * [create_tag 添加标签]
     * @param  Tag    $Tag [标签模型]
     * @param  userId [用户ID]
     * @param  name [标签名称]
     * @return 
     */
    public function create_tag(Tag $Tag)
    {
        $userId       = $this->getUserId();
       
        $data = $this->request->post();
        $data['userId'] = $userId;
        $info = $Tag->getInfo($data);
        if(empty($info)){
            $data['createTime'] = date('Y-m-d H:i:s',time());
            $re = $Tag->create_data($data);
            if($re){
                $this->success('添加成功',$re);
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->error('该标签已存在');
        }
       
    }

    /**
     * [update_tag 修改标签]
     * @param  Tag    $Tag [标签模型]
     * @param  userId [用户ID]
     * @param  name [标签名称]
     * @return 
     */ 
    public function update_tag(Tag $Tag)
    {
        $userId       = $this->getUserId();
        $data = $this->request->param();
        
        $info = $Tag->getInfo(['userId'=>$userId,'name'=>$data['name']]);
        $list = $Tag->getInfo(['id'=>$data['id']]);
        if($list['userId'] ==0){
 			$this->error('默认标签不能修改');
 			die;
        }
        if(empty($info)){
        	
            $re = $Tag->update_data($data);
            if($re){
                $this->success('成功',$data['name']);
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('该标签已存在');
        }
    }

    /**
     * [del_tag 删除标签]
     * @param  Tag    $Tag [标签模型]
     * @param  id [主键id]
     * @return 
     */
    public function del_tag(Tag $Tag)
    {
        $id = $this->request->param('id');
        $info = $Tag->find($id);
        if($info['userId'] ==0){
			$this->error('默认标签不能删除');
        }else{
        	$re = $Tag->destroy($id);
	        if($re){
	            $this->success('成功');
	        }else{
	            $this->error('失败');
	        }
        }
       
    }

}
